/*
 * Copyright 2008 - 2009 Lars Heuer (heuer[at]semagia.com). All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.semagia.atomico.server.dm.impl;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.semagia.atomico.MediaType;
import com.semagia.atomico.dm.IResource;
import com.semagia.atomico.server.dm.IFragmentInfo;

/**
 * {@link IFragmentInfo} implementation.
 * 
 * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a>
 * @version $Rev: 132 $ - $Date: 2012-07-20 16:55:23 +0000 (Fri, 20 Jul 2012) $
 */
public final class FragmentInfo extends AbstractMediaTypeAwareEntity 
    implements IFragmentInfo {

    private final Set<IResource> _resources;
    private final String _fragmentId;

    /**
     * Creates an instance which was generated by a single subject and has
     * one associated media type.
     *
     * @param id The entity id, an IRI.
     * @param title The title.
     * @param updated The time when the fragment was modified.
     * @param mediatype The media type of the fragment.
     * @param fragmentId The fragment id.
     * @param resource The subject which caused the generation of this fragment.
     */
    public FragmentInfo(String id, String title, long updated, 
            MediaType mediatype, String fragmentId, IResource resource) {
        this(id, title, updated, Collections.singletonList(mediatype), fragmentId, Collections.singleton(resource));
    }

    /**
     * Creates an instance which was generated by a single subject and has
     * multiple associated media types.
     *
     * @param id The entity id, an IRI.
     * @param title The title.
     * @param updated The time when the fragment was modified.
     * @param mediatypes The media types of the fragment.
     * @param fragmentId The fragment id.
     * @param resource The subject which caused the generation of this fragment.
     */
    public FragmentInfo(String id, String title, long updated, 
            List<MediaType> mediatypes, String fragmentId, IResource resource) {
        this(id, title, updated, mediatypes, fragmentId, Collections.singleton(resource));
    }

    /**
     * Creates an instance which was generated by multiple subjects and has
     * multiple associated media types.
     *
     * @param id The entity id, an IRI.
     * @param title The title.
     * @param updated The time when the fragment was modified.
     * @param mediatypes The media types of the fragment.
     * @param fragmentId The fragment id.
     * @param resources The subjects which caused the generation of this fragment.
     */
    public FragmentInfo(String id, 
            String title, long updated, List<MediaType> mediatypes, String fragmentId, 
            Set<IResource> resources) {
        super(id, title, updated, mediatypes);
        _fragmentId = fragmentId;
        if (resources == null || resources.isEmpty()) {
            throw new IllegalArgumentException("The resources must neither be null nor empty");
        }
        _resources = Collections.unmodifiableSet(new HashSet<IResource>(resources));
    }

    /* (non-Javadoc)
     * @see com.semagia.atomico.server.dm.IFragmentInfo#getResources()
     */
    @Override
    public Set<IResource> getResources() {
        return _resources;
    }

    /* (non-Javadoc)
     * @see com.semagia.atomico.server.dm.IFragmentInfo#getFragmentId()
     */
    @Override
    public String getFragmentId() {
        return _fragmentId;
    }

}
